package com.cnt.controller;

import com.cnt.entity.Report;
import com.cnt.service.ReportService;
import com.cnt.vo.ReportDonationVo;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.util.List;

@RestController
@RequestMapping("/api/report")
public class ReportController {

    @Autowired
    private ReportService reportService; // 假设有相应的Service层处理业务逻辑

    @PostMapping("/create")
    public ResultVo<String> createReport(@RequestBody Report report) {
        // 处理创建举报的业务逻辑
        return reportService.createReport(report);
    }

    @GetMapping("/reportDonationList")
    public ResultVo<List<ReportDonationVo>> getReportDonationList(@RequestParam(value = "minAge", required = false) Integer minAge,
                                                                  @RequestParam(value = "oldAge", required = false) Integer oldAge,
                                                                  @RequestParam(value = "name", required = false) String name) {
        // 处理查询反欺诈大厅的业务逻辑
        return reportService.getReportDonationList(minAge, oldAge, name);
    }

    @GetMapping("/queryReportByUserId")
    public ResultVo<List<ReportDonationVo>> queryReportByUserId(@RequestParam("userId") Integer userId) {
        // 处理用户查看自己发起的举报状态的业务逻辑
        return reportService.queryReportByUserId(userId);
    }

    @PostMapping("/reject")
    public ResultVo<String> rejectReport(@RequestParam("reportId") Integer reportId) {
        // 处理管理员进行评价的业务逻辑
        return reportService.rejectReport(reportId);
    }

    @PostMapping("/upload")
    public ResultVo<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("reportId") Integer reportId) {
        // 处理上传文件的业务逻辑
        return reportService.uploadFile(file, reportId);
    }

    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam("reportId") Integer reportId) {
        // 处理下载文件的业务逻辑
        try (BufferedInputStream bis = new BufferedInputStream(reportService.downloadFile(reportId), 4096)) {
            byte[] data = new byte[bis.available()];
            if(data.length==0){
                return ResponseEntity.status(500).body(null);
            }
            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=Report.pdf")
                    .body(data);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }

    @PostMapping("/changeStateByAdmin")
    public ResultVo<String> changeStateByAdmin(@RequestParam("reportId") Integer reportId) {
        // 改状态，改未处理<->已处理
        return reportService.changeStateByAdmin(reportId);
    }

    @PostMapping("/changeStateBySuperAdmin")
    public ResultVo<String> changeStateBySuperAdmin(@RequestParam("reportId") Integer reportId) {
        // 改状态，改未处理<->已处理
        return reportService.changeStateBySuperAdmin(reportId);
    }

}
